package com.javaketang.dky.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javaketang.dky.entity.EasybuyProduct;
import com.javaketang.dky.entity.EasybuyProductCategory;
import com.javaketang.dky.entity.EasybuyUser;
import com.javaketang.dky.entity.ShoppingCar;
import com.javaketang.dky.mapper.EasybuyProductCategoryMapper;
import com.javaketang.dky.mapper.EasybuyProductMapper;
import com.javaketang.dky.mapper.ShoppingCarMapper;
import com.javaketang.dky.service.EasybuyProductCategoryService;
import com.javaketang.dky.service.impl.EasybuyProductCategoryServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author dky
 * @since 2021-07-06
 */
@Controller
@RequestMapping("category")
public class EasybuyProductCategoryController {

    /**
     * 创建分类对象
     */
    @Resource
    private EasybuyProductCategoryServiceImpl productCategory;

    //商品类
    @Resource
    private EasybuyProductCategoryMapper categoryMapper;

    //购物车类
    @Resource
    private ShoppingCarMapper carMapper;

    //商品类
    @Resource
    private EasybuyProductMapper productMapper;

    /**
     * 分类管理方法
     *
     * @return
     */
    @RequestMapping("/classifyManage")
    public String classifyManage(Integer current, HttpSession session, Model model) {

        session.setAttribute("action", "classifyManage");

        //分页
        Page<EasybuyProductCategory> page = new Page<>(current == null ? 1 : current, 10);

        //查询分类管理
        List list = productCategory.queryAllProductCategory(page);

        //获取总页数
        int sumDateNumber = (int) (page.getTotal() % 10 == 0 ? page.getTotal() / 10 : page.getTotal() / 10 + 1);
        model.addAttribute("lists", list);
        model.addAttribute("sumPage", sumDateNumber);
        model.addAttribute("current", current == null ? 1 : current);


        return "Member_User";
    }


    /**
     * 查询分类
     *
     * @return
     */
    @RequestMapping("/selectProductCategory")
    @ResponseBody
    public Map<String, Object> selectProductCategory(Integer type) {

        System.out.println("类型--》》》" + type);

        //创建map集合
        Map<String, Object> map = new HashMap<>();

        //判断是一级和二级
        if (type != null && type == 2 || type == 3) {

            List<EasybuyProductCategory> list = categoryMapper.selectList(new QueryWrapper<EasybuyProductCategory>().eq("type", 1));

            map.put("list", list);


        }
        map.put("status", 1);


        return map;
    }

    /**
     * 查询二级分类
     *
     * @return
     */
    @RequestMapping("/selectProductCategoryLeve2")
    @ResponseBody
    public Map<String, Object> selectProductCategoryLeve2(Integer id) {

        //创建map集合
        Map<String, Object> map = new HashMap<>();

        List<EasybuyProductCategory> list = categoryMapper.selectList(new QueryWrapper<EasybuyProductCategory>().eq("parentId", id));

        map.put("list", list);

        map.put("status", 1);


        return map;
    }

    /**
     * 添加分类
     *
     * @param type     类型
     * @param parentId 父级id
     * @param name     分类名称
     * @return
     */
    @RequestMapping("/addProductCategory")
    @ResponseBody
    public Map<String, Object> addProductCategory(Integer type, Integer parentId, String name) {

        //创建map集合
        Map<String, Object> map = new HashMap<>();

        //创建分类对象
        EasybuyProductCategory productCategory = new EasybuyProductCategory();

        productCategory.setType(type);
        productCategory.setParentId(parentId);
        productCategory.setName(name);

        int row = categoryMapper.insert(productCategory);

        if (row > 0) {

            map.put("status", 1);
            map.put("message", "添加成功");
        } else {

            map.put("status", 0);
            map.put("message", "添加失败");

        }

        return map;
    }

    /**
     * 删除分类管理
     *
     * @param id
     * @return
     */
    @RequestMapping("/deleteProductCategory")
    @ResponseBody
    public Map<String, Object> deleteProductCategory(Integer id) {

        //创建map集合
        Map<String, Object> map = new HashMap<>();

        //根据id查询
        List<EasybuyProductCategory> list = productCategory.queryCategoryById(id);

        //赋值到对象中
        EasybuyProductCategory category = list.get(0);

        //判断为空时，说明没有子分类
        if (category == null) {

            //删除分类管理
            int row = categoryMapper.deleteById(id == null ? 0 : id);

            //判断删除成功时
            if (row > 0) {
                map.put("message", "删除成功");
                map.put("status", 1);
                return map;
            } else {
                map.put("message", "删除失败，服务器内部错误，请联系管理员");
                map.put("status", 0);
                return map;
            }

        } else {
            map.put("message", "已存在子分类，不能删除！");
            map.put("status", 0);
        }

        return map;
    }

    /**
     * 查询分类
     *
     * @return
     */
    @RequestMapping("/queryProduct")
    @ResponseBody
    public Map<String, Object> queryProduct(Integer type, Integer id) {

        //创建map集合
        Map<String, Object> map = new HashMap<>();

        //根据id查询商品
        EasybuyProductCategory category = categoryMapper.selectById(id);

        //判断是一级和二级
        if (type != null && type == 2 || type == 3) {

            List<EasybuyProductCategory> list = categoryMapper.selectList(new QueryWrapper<EasybuyProductCategory>().eq("type", 1));
            List<EasybuyProductCategory> list2 = categoryMapper.selectList(new QueryWrapper<EasybuyProductCategory>().eq("type", 2));

            //查询父级
            EasybuyProductCategory category1 = categoryMapper.selectById(category.getParentId());

            //查询父级的父级
            EasybuyProductCategory category2 = categoryMapper.selectById(category1.getParentId());

            System.out.println("=====" + category2);

            map.put("list", list);
            map.put("list2", list2);
            map.put("category1", category1);
            map.put("category2", category2);

        }
        map.put("category", category);
        map.put("status", 1);


        return map;
    }

    /**
     * 修改分类
     *
     * @param category              商品对象
     * @param productCategoryLevel1 一级分类
     * @param productCategoryLevel2 二级分类
     * @return
     */
    @RequestMapping("/productCategory")
    @ResponseBody
    public Map<String, Object> productCategory(EasybuyProductCategory category, Integer productCategoryLevel1, Integer productCategoryLevel2) {

        //创建map集合
        Map<String, Object> map = new HashMap<>();

        System.out.println(category);

        System.out.println(productCategoryLevel1 + "\t" + productCategoryLevel2);

        //类型为1时修改一级分类
        if (category.getType() == 1 || category.getType() == 2) {

            //创建商品对象
            EasybuyProductCategory productCategory = new EasybuyProductCategory();
            productCategory.setId(category.getId());//赋值id
            productCategory.setName(category.getName());//赋值name

            //判断需要修改父级id时
            if (category.getType() == 2) {
                productCategory.setParentId(productCategoryLevel1);  //父级id

            }

            int i = categoryMapper.updateById(productCategory);

            //判断修改成功
            if (i > 0) {


                String categoryLevel = "categoryLevel" + category.getType() + "Id";

                //查询商品
                List<EasybuyProduct> easybuyProducts = productMapper.selectList(new QueryWrapper<EasybuyProduct>().eq(categoryLevel, category.getId()));

                System.out.println("商品集合：" + easybuyProducts);
                System.out.println("商品集合长度：" + easybuyProducts.size());

                //用来获取商品id
                List<Integer> listProductId = new ArrayList<>();

                //判断有这个商品
                if (easybuyProducts.size() > 0) {


                    //创建商品对象
                    EasybuyProduct product = new EasybuyProduct();

                    if (category.getType() == 1) {
                        product.setCategoryLevel1Id(category.getId());
                    } else {
                        product.setCategoryLevel1Id(productCategoryLevel1);

                    }

                    System.out.println("赋值后的商品对象：" + listProductId);

                    int update = productMapper.update(product, new QueryWrapper<EasybuyProduct>().eq(categoryLevel, category.getId()));

                    //判断修改商品成功
                    if (update > 0) {
                        map.put("status", 1);
                        map.put("message", "商品修改成功！");

                    } else {
                        map.put("status", 0);
                        map.put("message", "商品修改失败！");
                    }

                } else {

                    map.put("status", 1);
                    map.put("message", "商品修改成功！");
                }

            } else {

                map.put("status", 0);
                map.put("message", "修改失败！");

            }

            //类型为3时，修改自己的父级id:parentId
        } else if (category.getType() == 3) {

            //创建商品对象
            EasybuyProductCategory productCategory = new EasybuyProductCategory();
            productCategory.setId(category.getId());//赋值id
            productCategory.setName(category.getName());//赋值name
            productCategory.setParentId(productCategoryLevel2);  //父级id

            int i = categoryMapper.updateById(productCategory);

            //判断修改成功
            if (i > 0) {

                //创建商品对象1
                EasybuyProductCategory category1 = new EasybuyProductCategory();
                category1.setId(productCategoryLevel2);//赋值id
                category1.setParentId(productCategoryLevel1);  //父级id

                int i1 = categoryMapper.updateById(category1);

                //判断修改父级的父级
                if (i1 > 0) {


                    String categoryLevel = "categoryLevel" + category.getType() + "Id";

                    //查询商品
                    List<EasybuyProduct> easybuyProducts = productMapper.selectList(new QueryWrapper<EasybuyProduct>().eq(categoryLevel, category.getId()));

                    System.out.println("商品集合：" + easybuyProducts);
                    System.out.println("商品集合长度：" + easybuyProducts.size());

                    //用来获取商品id
                    List<Integer> listProductId = new ArrayList<>();


                    //判断有这个商品
                    if (easybuyProducts.size() > 0) {

                        //创建商品对象
                        EasybuyProduct product = new EasybuyProduct();
                        product.setCategoryLevel1Id(productCategoryLevel1);
                        product.setCategoryLevel2Id(productCategoryLevel2);
                        product.setCategoryLevel3Id(productCategory.getId());

                        System.out.println("赋值后的商品对象：" + listProductId);

                        int update = productMapper.update(product, new QueryWrapper<EasybuyProduct>().eq(categoryLevel, category.getId()));

                        //判断修改商品成功
                        if (update > 0) {

                            map.put("status", 1);
                            map.put("message", "商品修改成功！");
                        } else {
                            map.put("status", 0);
                            map.put("message", "商品修改失败！");
                        }

                    } else {
                        map.put("status", 1);
                        map.put("message", "商品修改成功！");
                    }


                } else {

                    System.out.println("修改失败！");
                    map.put("status", 0);
                    map.put("message", "修改失败！");

                }

            }
        }
        return map;
    }


}

